package sjtu.ist.warehouse4ws.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import sjtu.ist.warehouse4ws.model.Epcunits;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.service.FunctionpointService;
import sjtu.ist.warehouse4ws.service.PrivilegeService;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShowFunctionListAction extends ActionSupport {

	private FunctionpointService functionpointService;
	private PrivilegeService privilegeService;
	private List<Functionpoint> functionList = new ArrayList<Functionpoint>();
	
	//获得Function列表
	public String showFunctionList(){
		List<Functionpoint> templist=functionpointService.getFunctionpointList();
		this.functionList = new ArrayList<Functionpoint>();
		
		ActionContext actionContext=ActionContext.getContext();
		Map session=actionContext.getSession();		
		int userType=(Integer)(session.get("user_type"));
		String userCompany=session.get("user_company").toString();		
		
		for(Functionpoint f : templist){
			if(privilegeService.checkPrivilege(f.getFileId(),userCompany,userType, "read")){
				functionList.add(f);
			}
		}
		return SUCCESS;
	}
	
	@JSON(name = "totalFunction")
	public int getTotalFunction() {
		return this.functionList.size();
	}

	@JSON(name = "Function")
	public List<Functionpoint> getFunctionList() {
		return this.functionList;
	}
	
	// 注入FunctionpointService
	public void setFunctionpointService(FunctionpointService functionpointService) {
		this.functionpointService = functionpointService;
	}
	
	public void setPrivilegeService(PrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}
}